// 全局本地存储方法
const storage = {
	set(key, value) {
		uni.setStorageSync(key, value)
	},
	get(key) {
		uni.getStorageSync(key);
	},
	updata(key, value) {
		uni.setStorageSync(key, value)
	}
}
// 全局提示方法
function toast(title) {
	uni.showToast({
		title: title,
		duration: 2000,
		icon: 'none',
	});
}
// 延时函数
function timeOut(times) {
	return new Promise((res, reject) => {
		setTimeout(() => {
			res()
		}, times)
	})
}

function getDate(type) {
	const date = new Date();
	let year = date.getFullYear();
	let month = date.getMonth() + 1;
	let day = date.getDate();

	if (type === 'start') {
		year = year - 60;
	} else if (type === 'end') {
		year = year + 2;
	}
	month = month > 9 ? month : '0' + month;
	day = day > 9 ? day : '0' + day;
	return `${year}-${month}-${day}`;
}

function getTime(type) {
	const date = new Date();
	let year = date.getFullYear();
	let month = date.getMonth() + 1;
	let day = date.getDate();
	let hours = date.getHours();
	let min = date.getMinutes();
	let send = date.getSeconds();
	if (type === 'start') {
		year = year - 60;
	} else if (type === 'end') {
		year = year + 2;
	}
	month = month > 9 ? month : '0' + month;
	day = day > 9 ? day : '0' + day;
	return `${year}-${month}-${day} ${hours}:${min}:${send}`;
}
// 倒计时 返回  天 时 分 秒
// 倒计时
function timeStamp(times) {
	let timestamp = Date.parse(new Date());
	timestamp = timestamp / 1000;
	let letime = times - timestamp;
	let time = parseInt(letime);
	if (parseInt(letime) > 60) {
		let second = parseInt(letime) % 60;
		let min = parseInt(letime / 60);
		if (second < 10) {
			second = "0" + second
		}
		if (min < 10) {
			min = "0" + min
		}
		time = min + ":" + second;
		if (min > 60) {
			min = parseInt(letime / 60) % 60;
			let hour = parseInt(parseInt(letime / 60) / 60);
			if (hour < 10) {
				hour = "0" + hour
			}
			time = hour + ":" + min + ":" + second;
			if (hour >= 24) {
				hour = parseInt(parseInt(letime / 60) / 60) % 24;
				let day = parseInt(parseInt(parseInt(letime / 60) / 60) / 24);
				time = day + "天 " + hour + ":" + min + ":" + second;
			}
		}
	}
	if (parseInt(letime) <= 0) {
		time = "00:00"
	}
	return time;
}

// 时间处理
function ferDate(arg, type = 'YYYY-MM-DD h:m:s') {
	if (arg.toString().length == 10) {
		arg = arg * 1000;
		//如果date为13位不需要乘1000
		//[js时间戳长度是13位]，php，java等时间戳长度为10位
	}
	var date = new Date(arg);
	var year = date.getFullYear(); //获取年
	var mouth = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1; //月
	var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate(); //日
	var hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours(); //时
	var minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes(); //分
	var second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds(); //秒
	var week; //星期
	if (date.getDay() == 0) week = '星期日';
	if (date.getDay() == 1) week = '星期一';
	if (date.getDay() == 2) week = '星期二';
	if (date.getDay() == 3) week = '星期三';
	if (date.getDay() == 4) week = '星期四';
	if (date.getDay() == 5) week = '星期五';
	if (date.getDay() == 6) week = '星期六';
	if (type == 'YYYY-MM-DD h:m:s') {
		return year + '-' + mouth + '-' + day + ' ' + hour + ':' + minute + ':' + second;
	}
	if (type == 'h:m:s') {
		return hour + ':' + minute + ':' + second;
	}
	if (type == 'YYYY-MM-DD') {
		return year + '-' + mouth + '-' + day;
	}
	if (type == 'MM-DD') {
		return mouth + '-' + day;
	}
	if (type == 'MM-DD WEEK') {
		return mouth + '-' + day + ' ' + week;
	}
}
/* 获取当前时间几个小时后的时间 */
function getManyDate(hours) {
	// //console.log('Date.now();',Date.now()+3600000*hours);
	var date = new Date(Date.now()+3600000*hours);
	var year = date.getFullYear(); //获取年
	var mouth = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1; //月
	var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate(); //日
	var hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours(); //时
	var minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes(); //分
	var second = date.getSeconds() < 10 ? '00' + date.getSeconds() : date.getSeconds(); //秒
	var week; //星期
	if (date.getDay() == 0) week = '星期日';
	if (date.getDay() == 1) week = '星期一';
	if (date.getDay() == 2) week = '星期二';
	if (date.getDay() == 3) week = '星期三';
	if (date.getDay() == 4) week = '星期四';
	if (date.getDay() == 5) week = '星期五';
	if (date.getDay() == 6) week = '星期六';
	return year + '-' + mouth + '-' + day + ' ' + hour + ':' + minute + ':' + second;
}
function getSystemInfo(){
	 

}

module.exports = {
	toast,
	storage,
	getDate,
	getTime,
	timeStamp,
	ferDate,
	timeOut,
	getManyDate,
	getSystemInfo
}
